/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.om.dto.*;
import com.excesys.exsecs.om.entity.*;
import com.excesys.exsecs.om.mapper.*;
import com.excesys.exsecs.om.service.OmInspectionResultService;
import com.excesys.exsecs.om.vo.ExceptionVo;
import com.excesys.exsecs.om.vo.ResutlVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @date 2023-05-19
 */
@RequiredArgsConstructor
@Service
public class OmInspectionResultServiceImpl extends ServiceImpl<OmInspectionResultMapper, OmInspectionResult>
		implements OmInspectionResultService {

	private final OmInspectionResultMapper omInspectionResultMapper;

	private final OmInspectionPhotoMapper omInspectionPhotoMapper;

	private final OmICheckPointMapper omICheckPointMapper;

	private final OmICheckSheetMapper omICheckSheetMapper;

	private final OmICheckRouteMapper omICheckRouteMapper;

	private final OmInspectionItemsMapper omInspectionItemsMapper;

	@Override
	public ValueDto getList(ResultDto resultDto) {
		OmInspectionResult result = omInspectionResultMapper.getList(resultDto.getInspectionSheetId(),
				resultDto.getPointCode(), resultDto.getInspectionAreaClassification());
		List<InspectionDto> inspectionDtolist = new ArrayList<>();
		ValueDto valueDto = new ValueDto();
		if (ObjectUtil.isNotEmpty(result)) {
			// for(OmInspectionResult omInspectionResult :list){
			valueDto.setInspectionSheetId(result.getInspectionSheetId());
			valueDto.setInspectionAreaClassification(result.getInspectionAreaClassification());
			valueDto.setPointCode(result.getPointCode());
			if (StringUtils.isNotEmpty(result.getInspectionSheetId()))
				valueDto.setInspectionSheetId(result.getInspectionSheetId());
			else
				valueDto.setInspectionSheetId("");
			if (StringUtils.isNotEmpty(result.getEquipmentStatus()))
				valueDto.setEquipmentStatus(result.getEquipmentStatus());
			else
				valueDto.setEquipmentStatus("");
			if (StringUtils.isNotEmpty(result.getRemarks()))
				valueDto.setRemarks(result.getRemarks());
			else
				valueDto.setRemarks("");
			valueDto.setNfcTime(result.getNfcTime());
			// 1-故障 0-正常
			if ("1".equals(result.getEquipmentStatus())) {
				valueDto.setInspectionValue("0");
			}
			else {
				valueDto.setInspectionValue("1");
			}
			if (StringUtils.isNotEmpty(result.getInspectionId())) {
				String[] ids = result.getInspectionId().split(",");
				for (int i = 0; i < ids.length; i++) {
					String[] info = ids[i].split("-");
					InspectionDto inspectionDto = new InspectionDto();
					for (int j = 0; j < info.length; j++) {
						if (j % 2 == 0) {
							if (StringUtils.isNotEmpty(info[j]))
								inspectionDto.setInspectionId(info[j]);
						}
						else {
							if (StringUtils.isNotEmpty(info[j]))
								inspectionDto.setInspectionValue(info[j]);
						}

					}
					inspectionDtolist.add(inspectionDto);
				}
			}
			valueDto.setFlag(result.getFlag());
			// }
			List<OmInspectionPhoto> photos = omInspectionPhotoMapper.getList(resultDto.getInspectionSheetId(),
					resultDto.getPointCode(), resultDto.getInspectionAreaClassification());
			if (photos.size() > 0) {
				List<FileDto> fileList = new ArrayList<>();
				for (OmInspectionPhoto omInspectionPhoto : photos) {
					FileDto fileDto = new FileDto();
					fileDto.setUrl(omInspectionPhoto.getUrl());
					fileDto.setFileId(omInspectionPhoto.getFileId());
					fileList.add(fileDto);
				}
				valueDto.setFileList(fileList);
			}
			valueDto.setInspectionDtolist(inspectionDtolist);
		}
		return valueDto;
	}

	@Override
	public OmInspectionResult getResult(String inspectionAreaClassification, String pointCode,
			String inspectionSheetId) {
		return omInspectionResultMapper.getResult(inspectionSheetId, pointCode, inspectionAreaClassification);
	}

	@Override
	public OmInspectionResult getOmInspectionResultList(String pointCode, String inspectionSheetId) {
		OmInspectionResult omInspectionResult = omInspectionResultMapper.getOmInspectionResultList(pointCode,
				inspectionSheetId);
		return omInspectionResult;
	}

	@Override
	public OmInspectionResult getFlag(String pointCode, String inspectionSheetId) {
		return omInspectionResultMapper.getFlag(pointCode, inspectionSheetId);
	}

	@Override
	public IPage<ExceptionDto> getResultList(Page page, ResutlVo resultDto) {

		IPage<ExceptionDto> resultList = omInspectionResultMapper.getResultLists(page, resultDto);
		List<ExceptionDto> resultLists = resultList.getRecords();
		for (ExceptionDto result : resultLists) {
			OmICheckPoint point = omICheckPointMapper.getPointInfoById(Integer.valueOf(result.getPointCode()));
			if (ObjectUtil.isEmpty(point))
				result.setPointCode("");
			else
				result.setPointCode(point.getPointName());
			OmICheckSheet omICheckSheet = omICheckSheetMapper.getSheetByCheckNo(result.getInspectionSheetId());
			if (ObjectUtil.isEmpty(omICheckSheet))
				result.setCheckUserName("");
			else
				result.setCheckUserName(omICheckSheet.getCheckUserName());
			if (ObjectUtil.isEmpty(omICheckSheet))
				result.setIrId("");
			else
				result.setIrId(omICheckSheet.getRouteId().toString());
			if (ObjectUtil.isNotEmpty(omICheckSheet)) {
				OmICheckRoute omICheckRoute = omICheckRouteMapper.getRouteById(omICheckSheet.getRouteId().toString());
				if (ObjectUtil.isEmpty(omICheckRoute))
					result.setRouteName("");
				else
					result.setRouteName(omICheckRoute.getRouteName());
			}
			if (StringUtils.isNotEmpty(result.getInspectionId())) {
				String[] ids = result.getInspectionId().split(",");
				List<InspectionDto> inspectionDtolist = new ArrayList<>();
				for (int i = 0; i < ids.length; i++) {
					String[] info = ids[i].split("-");
					InspectionDto inspectionDto = new InspectionDto();
					for (int j = 0; j < info.length; j++) {
						if (j % 2 == 0) {
							if (StringUtils.isNotEmpty(info[j]))
								if (ObjectUtil.isNotEmpty(omInspectionItemsMapper.getItemsById(info[j])))
									inspectionDto.setInspectionId(
											omInspectionItemsMapper.getItemsById(info[j]).getEvenInfo());
								else
									inspectionDto.setInspectionId("");

						}
						else {
							if (StringUtils.isNotEmpty(info[j]))
								inspectionDto.setInspectionValue(info[j]);
						}
						if (info[j].equals("0")) {
							inspectionDtolist.add(inspectionDto);
						}
					}

					result.setInspectionDtolist(inspectionDtolist);
				}
			}
		}
		return resultList.setRecords(resultLists);
	}

	@Override
	public List<ExceptionVo> listBamSBuildings() {
		ResutlVo resultDto = new ResutlVo();
		IPage<ExceptionDto> bamSBuildings = getResultList(new Page<>(),resultDto);
		// 转换成execl 对象输出
		return bamSBuildings.getRecords().stream().map(buildings -> {
			ExceptionVo exceptionVo = new ExceptionVo();
			BeanUtil.copyProperties(buildings, exceptionVo);
			return exceptionVo;
		}).collect(Collectors.toList());
	}

}
